#coding=utf-8
#import libs 
import sys
import os
from   os.path import abspath, dirname
sys.path.insert(0,abspath(dirname(__file__)))
import SummaryExcel_cmd
import SummaryExcel_sty
import Fun
import EXUIControl
EXUIControl.FunLib = Fun
EXUIControl.G_ExeDir = Fun.G_ExeDir
EXUIControl.G_ResDir = Fun.G_ResDir
import tkinter
from   tkinter import *
import tkinter.ttk
import tkinter.font
from   PIL import Image,ImageTk

#Add your Varial Here: (Keep This Line of comments)
#Define UI Class
class  SummaryExcel:
    def __init__(self,root,isTKroot = True,params=None):
        uiName = Fun.GetUIName(root,self.__class__.__name__)
        self.uiName = uiName
        Fun.Register(uiName,'UIClass',self)
        self.root = root
        self.configure_event = None
        self.isTKroot = isTKroot
        self.firstRun = True
        self.rootZoomed = False
        Fun.G_UIParamsDictionary[uiName]=params
        Fun.G_UICommandDictionary[uiName]=SummaryExcel_cmd
        Fun.Register(uiName,'root',root)
        style = SummaryExcel_sty.SetupStyle(isTKroot)
        self.UIJsonString = '{"Version": "1.0.0", "UIName": "SummaryExcel", "Description": "", "WindowSize": [636, 548], "WindowPosition": "Center", "WindowHide": false, "WindowResizable": true, "WindowTitle": "Excel表格汇总工具", "DarkMode": false, "BorderWidth": 0, "BorderColor": "#ffffff", "DropTitle": false, "DragWindow": false, "MinSize": [0, 0], "ResolutionScaling": false, "PopupDebugDialog": false, "TransparentColor": null, "RootTransparency": 255, "ICOFile": null, "WinState": 1, "WinTopMost": false, "BGColor": "#AAB6D3", "GroupList": {}, "WidgetList": [{"Type": "Form", "Index": 1, "AliasName": "Form_1", "BGColor": "#AAB6D3", "Size": [636, 548], "PlaceInfo": null, "EventList": {"Load": "Form_1_onLoad"}}, {"Type": "Button", "Index": 2, "AliasName": "get_data_range", "ParentName": "Form_1", "PlaceInfo": [478, 38, 105, 48, "nw", true, true], "Visible": true, "Size": [105, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "获取", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "get_data_range_onCommand"}}, {"Type": "Button", "Index": 7, "AliasName": "add_condition_label", "ParentName": "Form_1", "PlaceInfo": [479, 137, 105, 48, "nw", true, true], "Visible": true, "Size": [105, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "添加", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "add_condition_label_onCommand"}}, {"Type": "Button", "Index": 10, "AliasName": "add_summary_labels", "ParentName": "Form_1", "PlaceInfo": [479, 242, 105, 48, "nw", true, true], "Visible": true, "Size": [105, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "添加", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "add_summary_labels_onCommand"}}, {"Type": "Button", "Index": 15, "AliasName": "summary", "ParentName": "Form_1", "PlaceInfo": [49, 457, 133, 48, "nw", true, true], "Visible": true, "Size": [133, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "开始汇总", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "summary_onCommand"}}, {"Type": "Button", "Index": 17, "AliasName": "suggest", "ParentName": "Form_1", "PlaceInfo": [450, 459, 133, 48, "nw", true, true], "Visible": true, "Size": [133, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "推荐", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "suggest_onCommand"}}, {"Type": "Button", "Index": 18, "AliasName": "help", "ParentName": "Form_1", "PlaceInfo": [249, 457, 133, 48, "nw", true, true], "Visible": true, "Size": [133, 48], "BGColor": "#7186C7", "ActiveBGColor": "#FFFFFF", "Text": "帮助", "FGColor": "#ffffff", "ActiveFGColor": "#7186C7", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "Relief": "flat", "EventList": {"Command": "help_onCommand"}}, {"Type": "Label", "Index": 3, "AliasName": "Label_1", "ParentName": "Form_1", "PlaceInfo": [50, 40, 100, 48, "nw", true, true], "Visible": true, "Size": [100, 48], "BGColor": "#AAB6D3", "Text": "数据区域", "FGColor": "#495057", "Font": ["微软雅黑", 16, "normal", "roman", 0, 0]}, {"Type": "Label", "Index": 5, "AliasName": "Label_2", "ParentName": "Form_1", "PlaceInfo": [50, 136, 100, 48, "nw", true, true], "Visible": true, "Size": [100, 48], "BGColor": "#AAB6D3", "Text": "汇总标签", "FGColor": "#495057", "Font": ["微软雅黑", 16, "normal", "roman", 0, 0]}, {"Type": "Label", "Index": 8, "AliasName": "Label_3", "ParentName": "Form_1", "PlaceInfo": [50, 240, 100, 48, "nw", true, true], "Visible": true, "Size": [100, 48], "BGColor": "#AAB6D3", "Text": "求和标签", "FGColor": "#495057", "Font": ["微软雅黑", 16, "normal", "roman", 0, 0]}, {"Type": "Label", "Index": 11, "AliasName": "Label_4", "ParentName": "Form_1", "PlaceInfo": [49, 356, 100, 48, "nw", true, true], "Visible": true, "Size": [100, 48], "BGColor": "#AAB6D3", "Text": "序号列名", "FGColor": "#495057", "Font": ["微软雅黑", 16, "normal", "roman", 0, 0]}, {"Type": "Entry", "Index": 4, "AliasName": "data_range", "ParentName": "Form_1", "PlaceInfo": [201, 38, 240, 48, "nw", true, true], "Visible": true, "Size": [240, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#f8f9fa", "FGColor": "#000000", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Entry", "Index": 6, "AliasName": "condition_labels", "ParentName": "Form_1", "PlaceInfo": [201, 136, 240, 48, "nw", true, true], "Visible": true, "Size": [240, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#f8f9fa", "FGColor": "#000000", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Entry", "Index": 9, "AliasName": "summary_labels", "ParentName": "Form_1", "PlaceInfo": [201, 241, 240, 48, "nw", true, true], "Visible": true, "Size": [240, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#f8f9fa", "FGColor": "#000000", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Entry", "Index": 12, "AliasName": "index_column", "ParentName": "Form_1", "PlaceInfo": [201, 357, 240, 48, "nw", true, true], "Visible": true, "Size": [240, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#f8f9fa", "Text": "序号", "FGColor": "#000000", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "CheckButton", "Index": 14, "AliasName": "delete_zero", "ParentName": "Form_1", "PlaceInfo": [476, 361, 119, 41, "nw", true, true], "Visible": true, "Size": [119, 41], "Value": false, "BGColor": "#AAB6D3", "ActiveBGColor": "#AAB6D3", "Text": "  删除0值", "FGColor": "#000000", "ActiveFGColor": "#000000", "Anchor": "w", "Font": ["Microsoft YaHei UI", 16, "normal", "roman", 0, 0], "SelectColor": "#AAB6D3", "SelectBGColor": "#AAB6D3", "SelectFGColor": "#000000", "Relief": "flat", "SelfDrawing": true}]}'
        Form_1 = Fun.CreateUIFormJson(uiName,root,isTKroot,style,self.UIJsonString)
        #Inital all element's Data 
        Fun.InitElementData(uiName)
        #Call Form_1's OnLoad Function
        Fun.RunForm1_CallBack(uiName,"Load",SummaryExcel_cmd.Form_1_onLoad)
        #Add Some Logic Code Here: (Keep This Line of comments)



        #Exit Application: (Keep This Line of comments)
        if self.isTKroot == True and Fun.GetElement(self.uiName,"root"):
            self.root.protocol('WM_DELETE_WINDOW', self.Exit)
            self.root.bind('<Configure>', self.Configure)
            if self.rootZoomed == True and isinstance(self.root,tkinter.Tk) == True:
                self.root.state("zoomed")
                Fun.SetUIState(uiName,"zoomed")
                self.rootZoomed = False
            
    def GetRootSize(self):
        return Fun.GetUIRootSize(self.uiName)
    def GetAllElement(self):
        return Fun.G_UIElementDictionary[self.uiName]
    def Escape(self,event):
        if Fun.AskBox('提示','确定退出程序？') == True:
            self.Exit()
    def Exit(self):
        if self.isTKroot == True:
            Fun.DestroyUI(self.uiName,0,'')

    def Configure(self,event):
        Form_1 = Fun.GetElement(self.uiName,'Form_1')
        if Form_1 == event.widget:
            Fun.ReDrawCanvasRecord(self.uiName)
        if self.root == event.widget and (self.configure_event is None or self.configure_event[2]!= event.width or self.configure_event[3]!= event.height):
            uiName = self.uiName
            self.configure_event = [event.x,event.y,event.width,event.height]
            Fun.ResizeRoot(self.uiName,self.root,event)
            Fun.ResizeAllChart(self.uiName)
            pass
#Create the root of tkinter 
if  __name__ == '__main__':
    Fun.RunApplication(SummaryExcel)
